#include "stdafx.h"

extern WSASetLastError_proxy_t pTrueWSASetLastError;

int getsockopt_proxy (
    __in    SOCKET        s,
    __in    int           level,
    __in    int           optname,
    __out   char*         optval,
    __inout int*          optlen
    )
{
//SEND
    Header header(GetCurrentThreadId(), "getsockopt");
    getsockopt_in_inout getsockoptIN(s, level, optname, optlen);
    
    std::vector<char> out_buffer;
    Serialize(header, getsockoptIN, out_buffer);

    SendToClient (out_buffer);
    
//RECEIVE()
    getsockopt_out_inout getsockoptOUT;

    std::vector<char> in_buffer;
    
    RecvFromClient (in_buffer, header);
    Deserialize deserialize(in_buffer);
    deserialize(getsockoptOUT);

    if(getsockoptOUT.wsa_last_error_ != 0)
    {
        pTrueWSASetLastError(getsockoptOUT.wsa_last_error_);
        return SOCKET_ERROR;
    }
    
    if (optlen)
    {
        *optlen = *getsockoptOUT.optlen_;
    }
    if (optval && optlen)
    {
        memcpy (optval, getsockoptOUT.optval_, *getsockoptOUT.optlen_);
    }
    return 0;
}
